import kotlinx.coroutines.*
import org.junit.jupiter.api.Test
import java.lang.Thread.sleep
import kotlin.system.measureTimeMillis

/**
 * @Author ZhouHang
 * @Date 24/7/5 周五
 */
class NetworkTest {
    @Test fun `Test Async Network`() {
        runBlocking {
            val time = measureTimeMillis {
                val sum = withContext(Dispatchers.IO) {
                    val one = async { one() }
                    val two = async { two() }
                    one.await() + two.await()
                }
                println("两个方法返回值的和：${sum}")
            }
            println("执行耗时：${time}")
        }
        println("----------------")
    }

    fun one(): Int {
        sleep(1000)
        return 1
    }

    fun two(): Int {
        sleep(1000)
        return 2
    }
}